<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
Use DB;
use App\Http\Model\Cate;
use App\Http\Model\Goods;
use App\Http\Model\Home_user;
use App\Http\Model\Type;

class GoodsController extends Controller
{
    /**
     * 引入商品列表页
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        // $data = DB::table('sad_goods')->orderBy('ctime','desc')->get();
        $count = $request -> input('count',5);
        $search = $request -> input('search','');
        $request = $request -> all();
        $data = Goods::where('title','like','%'.$search.'%')->paginate($count);
        // $type = self::getCates();

        //将商品列表中的uid替换为卖家姓名
        foreach($data as $k=>$v){
          
            @$info = Goods::find($v['id'])->home_user->username;
            // dd($info);
            // $arr = $v->toArray();
            // dd($arr);
            // $arr['uid'] = $info;
            $data[$k]['uid'] = $info;
        }

        //将商品列表中的tid替换为板块名称
        foreach($data as $k=>$v){
           @$info = Goods::find($v['id'])->type->name;
            $data[$k]['tid'] = $info;


            //将商品列表中的condition值更改输出
            $c = '';
            switch ($v['condition']) {
                case '0':
                    $c = '九九新';
                    break;
                case '1':
                
                    $c = '九成新';
                    break;
                case '2':
                    $c = '八成新';
                    break;
                case '3':
                    $c = '七成新';
                    break;
                
                case '4':
                    $c = '垃圾成色';
                    break;
                
            }
            $data[$k]['condition'] = $c;
        }

        //引入商品列表页
        return view('admin.goods.index',['data'=>$data,'request'=>$request]);

    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
       $data = [];
       $re =  Goods::find($id)->delete();
//        删除成功
       if($re){
            $data['status']= 0;
            $data['msg']='删除成功';
       }else{
           $data['status']= 1;
           $data['msg']='删除失败';
       }
       return $data;
    }


    /**
     * 处理分类
     */
    static public function getCates()
    {

       
       $data = DB::table('sad_type')->select(DB::raw("*,concat(path,',',id) as paths"))->orderBy('paths')->get();
        foreach ($data as $key => $value) {
            // 统计一个字符串在另一个字符串出现次数
            $len = substr_count($value['path'],',');
            // $data[$key]['name']
            // |----男装
            // 重复的使用一个字符串
           $data[$key]['name'] = str_repeat('|----',$len).$value['name'];
        }
        return $data;
    }

    /**
     * 商品下架方法
     * author:马志达
     * time  :2017年8月30日14:09:14
     * @param:$id 商品的id
     *
     */
    public function changeStatus($id)
    {
        $data = Goods::find($id);
        if($data->status == 0){
            $data->status = 1;
        }else{
            $data->status = 0;
        }

        $res = $data->save();
        if($res){
            return redirect('admin/goods');
        }else{
            return redirect('admin/goods');
        }

    }
}
